/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.learners;

import org.osdtsystem.matrixparser.parsers.ParsingModel;
import org.osdtsystem.matrixparser.features.WeightVector;
import java.util.Map.Entry;
import org.osdtsystem.matrixparser.features.DenseFeatureVector;
import org.osdtsystem.matrixparser.features.Feature;
import org.osdtsystem.matrixparser.features.FeatureVector;
import org.osdtsystem.matrixparser.features.FeatureVectorUtils;

/**
 * 'upd' argument in update method should be:
 *    number of iteration * number of instances - (number of instances * (current iteration - 1) + (current instance + 1) + 1)
 *
 * @author Martin Haulrich
 */
public class PerceptronLearner implements Learner {
    ParsingModel model;
    DenseFeatureVector weights;
    DenseFeatureVector sumWeights;
    int updates;
    final boolean average;

    public PerceptronLearner(int paramSize) {
        this(paramSize, true);
    }

    public PerceptronLearner(int paramSize, boolean average) {
        this.average = average;
        weights = new DenseFeatureVector(paramSize);
        sumWeights = new DenseFeatureVector(paramSize);
        updates = 0;
    }

    public ParsingModel model() {
        return model;
    }

    @Override
    public WeightVector getWeightVector() {
        return weights;
    }

    @Override
    public void averageWeights() {
        double avVal = (double) updates;
        for (int i = 0; i < weights.size(); i++) {
            weights.setValue(i, sumWeights.getValue(i) / avVal);
        }
//        for (Feature f : sumWeights.allEntries()) {
//            weights.setWeight(f, sumWeights.getWeight(f) / avVal);
//        }
    }

    @Override
    public void update(FeatureVector goldVector, FeatureVector systemVector, Scorer scorer,
            double loss, double upd) {

        updates++;

        if (loss <= 0) {
            return;
        }

        FeatureVector distVector = FeatureVectorUtils.distance(goldVector, systemVector);

        for (Entry<Feature,Double> entry : distVector) {
            Feature f = entry.getKey();
            double fvalue = entry.getValue();
            double w = weights.getValue(f);
            weights.setValue(f, w + fvalue);
//            System.err.println(f.id + ": " + w + " -> " + (w+fv));

            double sW = sumWeights.getValue(f);
            sumWeights.setValue(f, sW + upd * fvalue);
        }
    }
}
